<script setup lang="ts">
// const checkboxes = document.querySelectorAll('input[type="checkbox"]');
//
// checkboxes.forEach(checkbox => {
//   checkbox.addEventListener('click', detectToggleOnce, { once: true });
// });

const detectToggleOnce = (e) => {
  e.target.classList.add('toggled-once');
};
</script>

<template>
  <div class="container">
    <h1>CheckBox Demo</h1>
    <p>This is a demo of the CheckBox component.</p>
    <p>You can click on the toggle CheckBox to see the animation.</p>

    <div class="toggle-container green">
      <input class="toggle-checkbox" type="checkbox" @click="detectToggleOnce">
      <div class="toggle-track">
        <!-- 圆形滚动球 -->
        <div class="toggle-thumb"></div>
      </div>
    </div>

    <div class="toggle-container blue">
      <input class="toggle-checkbox" type="checkbox" @click="detectToggleOnce">
      <div class="toggle-track">
        <div class="toggle-thumb"></div>
      </div>
    </div>

    <div class="toggle-container boo">
      <input class="toggle-checkbox" type="checkbox" @click="detectToggleOnce">
      <div class="toggle-track">
        <div class="toggle-thumb"></div>
      </div>
    </div>

  </div>
</template>

<style scoped lang="scss">
.container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  // gap: 1em; 是 CSS 中用于设置网格或弹性布局（Flexbox）的子元素间距的属性。它只是用于定义子元素之间的空间，而不是对子元素的内边距（padding）或外边距（margin）。
  // 1em：这是一个相对单位，表示与当前元素字体大小相同的长度。
  gap: 1em; /* 弹性子元素之间的间距为 1em */
  font-size: 2em;
}

.toggle-container {
  position: relative;
  border-radius: 50px;
  width: 52px;
  height: 30px;
}

.toggle-checkbox {
  -webkit-appearance: none;
  appearance: none;
  position: absolute;
  z-index: 1;
  border-radius: inherit;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

.toggle-track {
  display: flex;
  align-items: center;
  position: relative;
  border-radius: inherit;
  padding: 4px;
  width: 100%;
  height: 100%;
  background-color: #aeaeae;
  box-shadow: inset 0 1px 2px rgb(0 0 0 / .2);
  transition: background-color .4s linear;

  .toggle-container.green > .toggle-checkbox:checked + & {
    background-color: #4ccf59;
  }

  .toggle-container.blue > .toggle-checkbox:checked + & {
    background-color: #3384fb;
  }

  .toggle-container.boo > .toggle-checkbox:checked + & {
    background-color: #ff3372;
  }
}

.toggle-thumb {
  position: relative;
  border-radius: 11px;
  transform-origin: left;
  width: 22px;
  height: 22px;
  background-color: #fff;
  box-shadow:
      0 4px 4px rgb(0 0 0 / .2),
      inset 0 -2px 4px rgb(0 0 0 / .2),
;

  .toggle-checkbox.toggled-once + .toggle-track > & {
    animation-name: grow-out, bounce-out;
    animation-duration: .2s;
    animation-timing-function: cubic-bezier(.75, 0, 1, 1), cubic-bezier(0, 0, 0.3, 1.5);
    animation-delay: 0s, .2s;
    animation-fill-mode: forwards;
  }

  .toggle-checkbox.toggled-once:checked + .toggle-track > & {
    animation-name: grow-in, bounce-in;
  }

  .toggle-container.boo & {
    &::after {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      border-radius: inherit;
      width: 100%;
      height: 100%;
      background-image: url('https://assets.codepen.io/4175254/boo-face.png');
      background-size: calc(14 * 100 / 22 * 1%) calc(12 * 100 / 22 * 1%);
      background-position: center;
      background-repeat: no-repeat;
      box-shadow: inset 0 -2px 4px rgb(0 0 0 / .2);
      image-rendering: pixelated;
      opacity: 0;
      transition: opacity .2s linear;

      @at-root {
        .toggle-container.boo > .toggle-checkbox:checked + .toggle-track > .toggle-thumb::after {
          opacity: 1;
        }

        @media (hover: hover) {
          .toggle-container.boo > .toggle-checkbox:hover + .toggle-track > .toggle-thumb::after {
            opacity: 1;
          }
        }
      }
    }
  }
}

@keyframes grow-in {
  0% {
    border-radius: 11px;
    transform: translateX(0) scale(1);
  }
  100% {
    border-radius: calc(1 / (34 / 22) * 11px) #{'/'} calc(1 / (16 / 22) * 11px);
    transform: translateX(8px) scale(calc(34 / 22), calc(16 / 22));
  }
}

@keyframes bounce-in {
  0% {
    border-radius: calc(1 / (34 / 22) * 11px) #{'/'} calc(1 / (16 / 22) * 11px);
    transform: translateX(8px) scale(calc(34 / 22), calc(16 / 22));
  }
  100% {
    border-radius: 11px;
    transform: translateX(100%) scale(1);
  }
}

@keyframes grow-out {
  0% {
    border-radius: 11px;
    transform: translateX(100%) scale(1);
  }
  100% {
    border-radius: calc(1 / (34 / 22) * 11px) #{'/'} calc(1 / (16 / 22) * 11px);
    transform: translateX(2px) scale(calc(34 / 22), calc(16 / 22));
  }
}

@keyframes bounce-out {
  0% {
    border-radius: calc(1 / (34 / 22) * 11px) #{'/'} calc(1 / (16 / 22) * 11px);
    transform: translateX(2px) scale(calc(34 / 22), calc(16 / 22));
  }
  100% {
    border-radius: 11px;
    transform: translateX(0) scale(1);
  }
}
</style>